package org.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.micrometer.common.util.StringUtils;
import org.csu.mypetstore.api.common.CommonResponce;
import org.csu.mypetstore.api.entity.Category;
import org.csu.mypetstore.api.entity.Item;
import org.csu.mypetstore.api.entity.ItemInventory;
import org.csu.mypetstore.api.entity.Product;
import org.csu.mypetstore.api.persistence.CategoryMapper;
import org.csu.mypetstore.api.persistence.ItemInventoryMapper;
import org.csu.mypetstore.api.persistence.ItemMapper;
import org.csu.mypetstore.api.persistence.ProductMapper;
import org.csu.mypetstore.api.service.CatalogService;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ItemInventoryMapper itemInventoryMapper;

    @Override
    public CommonResponce<List<Category>> getCategoryList(){
        List<Category> categoryList = categoryMapper.selectList(null);
        if (categoryList.isEmpty()){
            return CommonResponce.createForSuccessMessage("没有分类信息");
        }
        return CommonResponce.createForSuccess(categoryList);
    }

    @Override
    public CommonResponce<Category> getCategory(String categoryId){
        Category category = categoryMapper.selectById(categoryId);
        if(category == null){
            return CommonResponce.createForSuccessMessage("没有该ID的Category");
        }
        return CommonResponce.createForSuccess(category);
    }

    @Override
    public CommonResponce<List<Product>> getProductListByCategoryId(String categoryId){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", categoryId);

        List<Product> productList = productMapper.selectList(queryWrapper);
        if(productList.isEmpty()){
            return CommonResponce.createForSuccessMessage("该分类下没有Product子分类");
        }
        return CommonResponce.createForSuccess(productList);
    }

    @Override
    public CommonResponce<Product> getProductById(String productId){
        Product product = productMapper.selectById(productId);
        if(product == null){
            return CommonResponce.createForError("没有该product的信息");
        }
        return CommonResponce.createForSuccess(product);
    }

    public CommonResponce<Item> getItemByItemid(String itemId){
        if (itemId==null||itemId.isEmpty()){
            return CommonResponce.createForError("itemid is null");
        }
        Item item=itemMapper.selectById(itemId);
        return CommonResponce.createForSuccess("成功",item);
    }
    @Override
    public CommonResponce<List<ItemVO>> getItemsByProductId(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productId" ,productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if(itemList.isEmpty()){
            return CommonResponce.createForError("该product下没有item商品");
        }
        Product product = productMapper.selectById(productId);
        List<ItemVO> itemVOList = new ArrayList<>();
        for(Item item : itemList){
            ItemVO itemVO = itemToItemVO(item, product);
            itemVOList.add(itemVO);
        }
        return CommonResponce.createForSuccess(itemVOList);
    }

    //搜索
    @Override
    public CommonResponce<List<Product>> searchProductByKey(String keyword) {
        if (keyword==null||keyword.isEmpty()){
            return CommonResponce.createForError(1,"关键词为空");
        }

        /// 调用服务层方法，根据关键字搜索产品
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.like("productid", keyword)
                .or()
                .like("category", keyword)
                .or()
                .like("name", keyword);

        List<Product> products = productMapper.selectList(wrapper);
        return CommonResponce.createForSuccess(products);
    }



    private ItemVO itemToItemVO(Item item , Product product){
        ItemVO itemVO = new ItemVO();
        itemVO.setItemId(item.getItemId());
        itemVO.setProductId((item.getProductId()));
        itemVO.setListPrice(item.getListPrice());
        itemVO.setUnitCost(item.getUnitCost());
        itemVO.setSupplierId(item.getSupplierId());
        itemVO.setStatus(item.getStatus());
        itemVO.setAttribute1(item.getAttribute1());
        itemVO.setAttribute2(item.getAttribute2());
        itemVO.setAttribute3(item.getAttribute3());
        itemVO.setAttribute4(item.getAttribute4());
        itemVO.setAttribute5(item.getAttribute5());

        itemVO.setCategoryId(product.getCategoryId());
        itemVO.setProductName(product.getName());
        itemVO.setProductDescription(product.getDescription());

        ItemInventory itemInventory = itemInventoryMapper.selectById(item.getItemId());
        itemVO.setQuantity(itemInventory.getQuantity());

        return itemVO;
    }
}
